//
//  CLRMLightView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/4.
//

#import "CLRMLightView.h"
@interface CLRMLightView ()
@property (nonatomic, strong) UIImageView *leftIcon;

@property (nonatomic, strong) UISlider *slider;

@property (nonatomic, strong) UIImageView *rightIcon;
@end
@implementation CLRMLightView

- (void)addSubviews {
    [super addSubviews];
    
    self.backgroundColor = UIColor.clearColor;

    
    UIImageView *leftIcon = [[UIImageView alloc]init];
    self.leftIcon = leftIcon;
    leftIcon.image = [[UIImage imageNamed:@"light_0"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    leftIcon.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:leftIcon];
    
    UIImageView *rightIcon = [[UIImageView alloc]init];
    self.rightIcon = rightIcon;
    leftIcon.image = [[UIImage imageNamed:@"light_1"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    rightIcon.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:rightIcon];
    
    // 进度条
    UISlider *slider = [[UISlider alloc]init];
    self.slider = slider;
    slider.minimumValue = 0.0;
    slider.maximumValue = 1.0;
    slider.value = [UIScreen mainScreen].brightness;
    [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:(UIControlEventValueChanged)];
    [slider setThumbImage:[[UIImage imageNamed:@"slider"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)] forState:(UIControlStateNormal)];
    // 设置当前进度颜色
    slider.minimumTrackTintColor = CL_READ_COLOR_MAIN;
    // 设置总进度颜色
    slider.maximumTrackTintColor = CL_READ_COLOR_MENU_COLOR;
    // 设置当前拖拽圆圈颜色
    slider.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:slider];
}

/// 滑块变化
- (void)sliderChanged:(UISlider *)slider {
    [UIScreen mainScreen].brightness = slider.value;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    
    CGFloat iconWH = CL_SPACE_SA_20;
    CGFloat iconY = (h - iconWH) / 2;
    self.leftIcon.frame = CGRectMake(0, iconY, iconWH, iconWH);
    self.rightIcon.frame = CGRectMake(w - iconWH, iconY, iconWH, iconWH);
    
    CGFloat sliderX = CGRectGetMaxX(self.leftIcon.frame) + CL_SPACE_SA_15;
    CGFloat sliderW = CGRectGetMinX(self.rightIcon.frame) - CL_SPACE_SA_15 - sliderX;
    self.slider.frame = CGRectMake(sliderX, 0, sliderW, h);
}

@end
